import * as  protobuf_1  from "@bufbuild/protobuf";
import * as ezgame_pb from "../pb/ezgame_pb";


export class EzGameUtils{
    /**
     * 将自定义对象的数组转成 Uint8Array
     * @param schema 数组元素的schema
     * @param data 数据数据
     * @returns 
     */
    public static objArrayEncode(schema:any,data:any[]){
        if(data){
            let byteArray:Uint8Array[] = []
            data.forEach(v=>{
                let objBytes =  (0, protobuf_1.toBinary)(schema, v);
                byteArray.push(objBytes);
            })
            let wraperData =  (0, protobuf_1.create)(ezgame_pb.ObjectListWrapperSchema,{value:byteArray});
            return (0, protobuf_1.toBinary)(ezgame_pb.ObjectListWrapperSchema, wraperData);
        }
        return new Uint8Array(0);
    }

    /**
     * 基本数据类型的转换，number，string,bool,long等
     * @param schema 
     * @param data 
     * @returns 
     */
    public static baseDataArrayEncode(schema:any,data:any[]){
        if(data){
            let wraperData =  (0, protobuf_1.create)(schema,{value:data});
            return (0, protobuf_1.toBinary)(schema, wraperData);
        }
        return new Uint8Array(0);
    }

    public static baseDataEncode(schema:any,data:any){
        if(data){
            let wraperData =  (0, protobuf_1.create)(schema,{value:data});
            return (0, protobuf_1.toBinary)(schema, wraperData);
        }
        return new Uint8Array(0);
    }

    public static objDataEncode(schema:any,data:any){
        if(data){
            return (0, protobuf_1.toBinary)(schema, data);
        }
        return new Uint8Array(0);
    }




    /**
     * Uint8Array转成将自定义对象的数组
     * @param schema 数组元素的schema
     * @param data 数据数据
     * @returns 
     */
    public static objArrayDecode(schema:any,data:Uint8Array){
        if(data){
            let array =  (0,protobuf_1.fromBinary)(ezgame_pb.ObjectListWrapperSchema,data).value
            if (!array || array.length == 0) {
                return [];
            }
            return array.map(item => {
                return (0, protobuf_1.fromBinary)(schema, item);
            });
        }
        return []
    }

    /**
     * Uint8Array转成 基本数据类型：number，string,bool,long等
     * @param schema 数组元素的schema
     * @param data 数据数据
     * @returns 
     */
    public static baseDataArrayDecode(schema:any,data:Uint8Array){
        if(data){
            let array =  (0,protobuf_1.fromBinary)(ezgame_pb.ObjectListWrapperSchema,data).value
            if (!array || array.length == 0) {
                return [];
            }
            return array.map(item => {
                return (0, protobuf_1.fromBinary)(schema, item);
            });
        }
        return []
    }


    public static baseDataDecode(schema:any,data:Uint8Array){
        if(data){
            return  ((0,protobuf_1.fromBinary)(schema,data) as any).value
        }
        return null
    }


    public static objDataDecode(schema:any,data:Uint8Array){
        if(data){
            return (0,protobuf_1.fromBinary)(schema,data)
        }
        return null
    }
}